//
//  BookManagedObject.m
//  Book Editor
//
//  Created by Chris Karr on 6/21/08.
//  Copyright 2008 Northwestern University. All rights reserved.
//

#import "BookManagedObject.h"

@implementation BookManagedObject

@synthesize coverImage;

#pragma mark Mutators & Accessors

- (void) save:(NSURL *) url
{

}

- (void) open:(NSURL *) url
{

}

- (NSString *) contributorsString
{
	NSMutableSet * contributors = [self mutableSetValueForKey:@"contributors"];
	
	NSMutableString * string = [NSMutableString stringWithString:@""];

	NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
	
	for (NSManagedObject * contributor in [[contributors allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]])
	{
		if (![string isEqualToString:@""])
			[string appendString:@"; "];

		NSString * name = [contributor valueForKey:@"name"];
		
		if (name != nil)
			[string appendString:name];
		else
			[string appendString:NSLocalizedString (@"UNKNOWN CONTRIBUTOR", nil)];

		NSString * role = [contributor valueForKey:@"role"];
		
		if (role != nil)
			[string appendFormat:@" (%@)", role];

	}
	
	[sort release];
	
	if ([string isEqualToString:@""])
		return nil;
	
	NSLog (@"string = %@", string);
	
	return string;
}

- (NSString *) publishedYear
{
	NSDateFormatter * yearFormatter = [[NSDateFormatter alloc] initWithDateFormat:@"%Y" allowNaturalLanguage:NO];
	
	NSDate * published = [self valueForKey:@"published"];
	
	if (published == nil)
		return NSLocalizedString (@"UNKNOWN YEAR", nil);
	
	NSString * year = [yearFormatter stringFromDate:published];
	
	[yearFormatter release];
	
	return year;
}

@end
